package controls;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pages.WebPage;
import reports.Report;
import utils.Events;

public class RadioButton {
	private WebElement radioButton;
	private By by;
	private Events events;

	/**
	 * Constructor of radio button
	 * 
	 * @author Pradeep Sundaram
	 * @param radioButtonName
	 * @param description
	 */
	public RadioButton(String radioButtonName, String description,
			ElementFinder util, Events events) {
		if (radioButtonName.startsWith("name")) {
			radioButton = util.findElementByName(radioButtonName);
		} else if (radioButtonName.startsWith("css")) {
			radioButton = util.findElementByCss(radioButtonName);
		} else if (radioButtonName.startsWith("//")) {
			radioButton = util.findElementByXpath(radioButtonName);
		} else if (radioButtonName.startsWith("id")) {
			radioButton = util.findElementByID(radioButtonName);
		} else if (radioButtonName.startsWith("(//")) {
			radioButton = util.findElementByXpath(radioButtonName);
		} else {
			Report.log("radio button is not found");
		}
		WebPage.elementList.put(radioButton, description);
	}

	/**
	 * Constructor for Check Box when By of the check box is required
	 * 
	 * @author Pradeep Sundaram
	 * @param radioButtonName
	 * @param byOfCheckBox
	 * @param description
	 */
	public RadioButton(String radioButtonName, By byOfRadio,
			String description, ElementFinder util) {
		if (radioButtonName.startsWith("name")) {
			radioButton = util.findElementByName(radioButtonName);
		} else if (radioButtonName.startsWith("css")) {
			radioButton = util.findElementByCss(radioButtonName);
		} else if (radioButtonName.startsWith("//")) {
			radioButton = util.findElementByXpath(radioButtonName);
		} else if (radioButtonName.startsWith("id")) {
			radioButton = util.findElementByID(radioButtonName);
		}
		by = byOfRadio;
		WebPage.elementList.put(radioButton, description);
	}

	/**
	 * This method will choose the radio button
	 * 
	 * @author Pradeep Sundaram
	 */
	public void choose() {
		events.choose(radioButton);

	}

	/**
	 * This method will return the By for the Check Box
	 * 
	 * @author Pradeep Sundaram
	 * @param elem
	 * @return
	 */
	public By getBy() {
		return by;
	}

	/**
	 * This method will return the webelement of the check box
	 * 
	 * @author Pradeep Sundaram
	 * @return
	 */
	public WebElement getWebElement() {
		return radioButton;
	}

	/**
	 * will return boolean based on the presence of the radio button
	 * 
	 * @return boolean
	 */
	public boolean isDisplayed() {
		Report.log("Checking whether the field "
				+ WebPage.elementList.get(radioButton) + " is displayed.<BR>");
		return radioButton.isDisplayed();
	}

	/**
	 * This method will return the tool tip of the radioButton
	 * 
	 * @author PSubramani33
	 * @return String
	 */
	public String getToolTip() {
		Report.log("Getting the tool tip of the button "
				+ WebPage.elementList.get(radioButton) + ".<BR>");
		return radioButton.getAttribute("title");
	}
}
